package com.oly.cms.general.taglib;

import java.util.List;

import com.oly.cms.common.model.support.PageData;
import com.oly.cms.general.model.po.WebArticle;
import com.oly.cms.general.service.cache.WebArticleCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 文章标签 涉及文章表的操作
 */
@Service("articleTag")
public class ArticleTag {
    @Autowired
    private WebArticleCacheService webPostService;

    /**
     * 获取文章Html
     * 
     * @param articleId
     * @return
     */
    public WebArticle getWebArticleHtmlById(long articleId) {
        return webPostService.getWebArticleHtmlById(articleId);
    }

    /**
     * 获取文章MarkDown
     * 
     * @param articleId
     * @return
     */
    public WebArticle getWebArticleMdById(long articleId) {
        return webPostService.getWebArticleMdById(articleId);
    }

    /**
     * 依据分类
     * 
     * @param catId
     * @param size
     * @param themeName
     * @return
     */
    public List<WebArticle> listWebArticlesByCatId(Long catId, Integer size, String themeName) {

        return webPostService.listWebArticlesByCatId(catId == null ? 0L : catId, size, themeName);
    }

    /**
     * 依据标签
     * 
     * @param tagId
     * @param size
     * @param themeName
     * @return
     */
    public List<WebArticle> listWebArticlesByTagId(Long tagId, Integer size, String themeName) {

        return webPostService.listWebArticlesByTagId(tagId == null ? 0L : tagId, size, themeName);
    }

    public PageData pagePosts(String themeName, int pageSize) {

        return this.pagePosts(themeName, 1, pageSize);
    }

    public PageData pagePosts(String themeName, int pageNum, int pageSize) {
        List<WebArticle> list = webPostService.listWebArticlesOrder(pageNum, pageSize, "create_time desc", themeName);
        PageData pageOne = PageData.getData(list);
        return pageOne;
    }

    /**
     * 文章类型
     * 
     * @param pageSize    获取条数
     * @param articleType 文章类型
     * @return
     */
    public List<WebArticle> listWebArticleByType(Byte articleType, int pageSize) {
        return this.sortType(articleType, pageSize, "create_time desc");
    }

    /**
     * 依据时间排序
     * 
     * @param pageSize
     * @param themeName
     * @return
     */
    public List<WebArticle> listWebArticleOrderByTime(int pageSize, String themeName) {
        return this.sort(pageSize, "create_time desc", themeName);
    }

    /**
     * 获取顶置文章
     * 
     * @param pageSize
     * @param themeName
     * @return
     */
    public List<WebArticle> listWebArticleOrderByTop(int pageSize, String themeName) {
        return this.sort(pageSize, "article_top desc", themeName);
    }

    private List<WebArticle> sort(int pageSize, String orderString, String themeName) {

        return webPostService.listWebArticlesOrder(pageSize, orderString, themeName);
    }

    private List<WebArticle> sortType(byte type, int pageSize, String orderString) {

        return webPostService.listWebArticlesByType(type, pageSize, orderString);
    }

}
